package utils;

import com.lefull.lefullbase.utils.helper.P;

import java.util.HashMap;
import java.util.Map;

import constant.C;

import static com.lefull.lefullbase.utils.helper.To.mMd5;
import static com.lefull.lefullbase.utils.helper.To.paseData;
import static com.lefull.lefullbase.utils.helper.To.sortOfString;

public class LeFullMD5Utils {
    LeFullMD5Utils() {

    }

    public static String md5Value(Map<String, String> arg) {
        if (arg == null || arg.isEmpty()) {
            return "";
        }
        //将签名的app-token改为token
        Map<String, String> currentMap = new HashMap<>();
        currentMap.putAll(arg);
        currentMap.put(C.TOKEN, currentMap.get(C.APP_TOKEN));
        currentMap.put(C.SECRET, P.getString(P.SECRET));
        currentMap.remove(C.APP_TOKEN);
        String[] keyArray = new String[0];
        keyArray = currentMap.keySet().toArray(keyArray);
        // 排序key
        keyArray = sortOfString(keyArray);
        StringBuffer sb = new StringBuffer();
        String md5 = "";
        for (int i = 0; i < keyArray.length; i++) {
            String key = keyArray[i];
            Object value = currentMap.get(key);
            // 拼接加密字符串
            sb.append(paseData(key, value) + "&");
        }
        md5 = sb.toString();
        md5 = mMd5(P.getString(P.SECRET).substring(5, 15) + md5.subSequence(0, md5.length() - 1));
        return md5;

    }
}
